/*
 *  cstring.cpp
 *  string
 *  implementation of cstring.h
 *
 *  Created by nobles0ul on 12/09/11.
 *  Copyright 2011 JIIT. All rights reserved.
 *
 */


#include<iostream>
#include<cstdlib>
#include<string>
#include"cstring.h"

using namespace std;


CString::CString( char *p)
{
	len =strlen(p);
	str = new char(len);
	strcpy(str,p);
}

int CString::substr( char *s)
{
	int l;
	
	char *pos;
	l=strlen(s);
	
	
	if(l>len)
	{
		return -1; // erroneous case
	}
	
	int i=0;
	
	char *temps = new char(l);
	while(i<(len-l+1))
	{
		int j;
		
		for(j=i;j<i+l;j++)
		{
			temps[j-i]=str[j];
		}
		temps[j]='\0';
		
		
		if(!strcmp(temps,s))
		{
			return 1;
		}
		i++;
		
	}
	return 0;
	
	
	
	
}

void CString::chgSize(int p)
{
	len = p;
	
	char * temp = new char(p);
	str = temp;
	
}

void CString::concat(char *p)
{
	int l;
	l=strlen(p);
	
	chgSize(l);
	strcat(str,p);
	
}

int CString::stringCompare(char *u)
{
	int i;
	i = strcmp(str,u);
	return i;
	
}

void CString::stringCpy(char *p)
{
	int l;
	l= strlen(p);
	chgSize(l);
	strcpy(str,p);
	
}

int CString::stringLen(void)
{
	int i;
	i=0;
	
	while(str[i]!='\0')
	{
		i++ ;
	}
	
	return (i);
	
	
}	

int CString::chrIndex(char c)
{
	int pos = -1;
	int i=0;
	 while(str[i++]!='\0')
	{
		if(str[i]==c)
		{
			pos = i;
			return i;
		}
	}
	
	return pos;
	
}

CString::~CString()
{
	delete str;
	str=NULL;
	
}


